package com.song.studylineserver.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import com.song.studylineserver.domain.Topic;
import com.song.studylineserver.service.TopicService;
import com.song.studylineserver.util.Result;

import com.song.studylineserver.util.UUID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "题目")
@RequestMapping("/topic/")
public class TopicController {
    @Autowired
    private TopicService topicService;

    @ApiOperation("获取题目")
    @RequestMapping(value = "getTopic/{id}")
    public  Result getTopicById(@PathVariable String id){
        return new Result(200,"获取成功",topicService.selectTopicByTopicId(id));
    }


    @ApiOperation("hello")
    @RequestMapping("hello")
    public Result hello(){
        return new Result(200,"成功了!",topicService.selectAll());
    }

    @ApiOperation("添加题目")
    @RequestMapping("add")
    public  Result addTopic(@RequestBody Topic topic) {
        if(topic.getTopicId().isEmpty()){
            topic.setTopicId(UUID.fastUUID().toString());
        }
        topic.setTopicAuthor(StpUtil.getLoginId().toString());
        topic.setTopicCreate(DateTime.now());
        topicService.saveOrUpdate(topic);
        return new Result(200, "添加题目成功", topic.getTopicId());

    }

    /**
     * 获取当前题库中所有题目的类型
     */
    @ApiOperation("添加题目")
    @RequestMapping("getAllType")
    public List<String> getAllType() {
        return  topicService.searchAllType();
    }



}
